package com.mylhyl.circledialog;

import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;

/**
 * Dialog封装类
 *
 * @since 2021-07-21
 */
public class BaseCircleDialog extends CommonDialog {
    private static final int WIDTH = 800;
    private final Context myContext;
    private final Window window;
    private int layout;
    private int type;
    private boolean isEnabled;
    private ViewConvertListener viewConvertListener;

    /**
     * BaseCircleDialog
     *
     * @param context 上下文
     */
    protected BaseCircleDialog(Context context) {
        super(context);
        myContext = context;
        window = getWindow();
    }

    /**
     * 初始化dialog
     *
     * @param context 上下文
     * @return NiceDialog 自定义Dialog
     */
    public static BaseCircleDialog init(Context context) {
        return new BaseCircleDialog(context);
    }

    /**
     * 设置gravity
     *
     * @param gravity ConstandCodes常量
     * @return NiceDialog 自定义Dialog
     */
    public BaseCircleDialog setGravity(int gravity) {
        switch (gravity) {
            case ConstandCodes.TOP:
                window.setLayoutAlignment(LayoutAlignment.TOP);
                break;
            case ConstandCodes.BOTTOM:
                window.setLayoutAlignment(LayoutAlignment.BOTTOM);
                break;
            case ConstandCodes.LEFT:
                break;
            case ConstandCodes.RIGHT:
                break;
            case ConstandCodes.CENTER:
                break;
            default:
                break;
        }
        return this;
    }

    /**
     * 设置Dialog布局接口
     *
     * @param viewConvertListener Dialog布局对外暴露接口
     * @return NiceDialog 自定义Dialog
     */
    public BaseCircleDialog setViewConvertListener(ViewConvertListener viewConvertListener) {
        this.viewConvertListener = viewConvertListener;
        return this;
    }

    /**
     * 设置layout资源文件
     *
     * @param layoutId 资源ID
     * @param types dialog类型
     * @return NiceDialog 自定义Dialog
     */
    public BaseCircleDialog setLayoutId(int layoutId, int types) {
        this.layout = layoutId;
        this.type = types;
        return this;
    }

    /**
     * 对话框是否允许返回键关闭
     *
     * @param isCancel true
     */
    public void setCancelable(boolean isCancel) {
        siteRemovable(isCancel);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        Component shareLayout = LayoutScatter.getInstance(myContext)
                .parse(layout, null, true);
        ViewHolder viewHolder = covertHolder(shareLayout, myContext);
        if (viewConvertListener != null) {
            viewConvertListener.convertView(viewHolder, this);
        }
        if (type == ConstandCodes.CONFRIMTYPE) {
            int height = shareLayout.getLayoutConfig().height;
            setSize(WIDTH, height);
        }
        setDialogListener(new DialogListener() {
            @Override
            public boolean isTouchOutside() {
                if (isEnabled) {
                    destroy();
                }
                return true;
            }
        });
        setContentCustomComponent(shareLayout);
    }

    /**
     * 设置是否点击外部关闭dialog
     *
     * @param isEnableds false为不关闭 true为关闭
     * @return NiceDialog 自定义Dialog
     */
    public BaseCircleDialog setCanceledOnTouchOutside(boolean isEnableds) {
        this.isEnabled = isEnableds;
        return this;
    }

    private ViewHolder covertHolder(Component view, Context context) {
        ViewHolder viewHolder = ViewHolder.create(view, context);
        return viewHolder;
    }
}

